home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu086.dms / pu086.adf / tools / ar.c < prev    next >
C/C++ Source or Header  |  1990-12-06  |  3KB  |  138 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided that:
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such.
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.   ar.c    within        Public Domain ar
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16.  
  17. /* This program links object files to create a library, at the moment
  18. this is very simplistic, it just concatenates the object files together,
  19. well it works! */
  20.  
  21. /* The program should take the names of the files and a -ooutname to
  22. tell us where to put it */
  23.  
  24. /* Link files together to create a library */
  25.  
  26. char name[32]="libc.a";
  27.  
  28. do_o(out)
  29.     char *out;
  30.    {/* change the output name */
  31.     strcpy(name,out);
  32.     }
  33.  
  34. static char sfile[128]="";
  35.  
  36. do_f(out)
  37.     char *out;
  38.    {/* Set up the file source */
  39.     strcpy(sfile,out);
  40.     }
  41.  
  42. copy_file(from,to)
  43.     FILE *from;
  44.     FILE *to;
  45.    {/* Copy a file */
  46.     char temp;
  47.     temp = fgetc(from);
  48.     while(!feof(from))
  49.        {
  50.         fputc(temp,to);
  51.         temp = fgetc(from);
  52.         }
  53.     }
  54.  
  55. do_sfile(out)
  56.     FILE *out;
  57.    {/* Scan a file for source names */
  58.     char name[32];
  59.     int  i;
  60.     FILE *list,*in;
  61.     
  62.     if(*sfile=='\0')
  63.         return;
  64.     list = fopen(sfile,"r");
  65.     while(!feof(list))
  66.        {/* Get the next filename */
  67.         i=-1;
  68.         do{i++;
  69.            name[i] = fgetc(list);
  70.            }
  71.         while(!feof(list) && isgraph(name[i]));
  72.     name[i]='\0';
  73.         if(i>0)
  74.            {in = fopen(name,"r");
  75.             if(in == NULL)
  76.                {printf("Cannot open %s\n",name);
  77.                 exit(10);
  78.                 }
  79.             copy_file(in,out);
  80.             fclose(in);
  81.             }
  82.         }
  83.     fclose(list);
  84.     }
  85.  
  86. struct _opts
  87.    {char ch;
  88.     int  (*fun)();
  89.     } Options[] = 
  90.    {{'o',do_o},
  91.     {'f',do_f},
  92.     {'\0'}};
  93.  
  94. main(argc,argv)
  95.     int argc;
  96.     char *argv[];
  97.    {/* first open the library spec file, passed as the first arg */
  98.     FILE *out,*in;
  99.     char file_name[128];
  100.     int  count;
  101.  
  102.     /* Find the output file name in the list */
  103.     for(count=1;count<argc;count++)
  104.        {if(argv[count][0]=='-')
  105.            {int i;
  106.             for(i=0;Options[i].ch!='\0' && 
  107.                     Options[i].ch!=tolower(argv[count][1]);i++);
  108.             if(Options[i].ch!='\0')
  109.                 (*(Options[i].fun))(&argv[count][2]);
  110.               else
  111.                {printf("ar -ooutfile -fsfile file1 file2 ...\n");
  112.                 exit(1);
  113.                 }
  114.             }
  115.         }
  116.  
  117.     out=fopen(name,"w");
  118.     if(out==NULL)
  119.        {printf("Cannot open \"%s\" for output\n",name);
  120.         exit(1);
  121.         }
  122.  
  123.     do_sfile(out);
  124.     for(count=1;count<argc;count++)
  125.        {if(argv[count][0]!='-')
  126.            {char temp;
  127.             in = fopen(argv[count],"r");
  128.             if(in==NULL)
  129.            {printf("Cannot open \"%s\" for input\n",argv[count]);
  130.                 exit(10);
  131.         }
  132.             copy_file(in,out);
  133.             fclose(in);
  134.             }
  135.         }
  136.     fclose(out);
  137.     }
  138.